package com.t282.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t282.crm.entity.CstLinkman;
import com.t282.crm.service.impl.CstLinkmanServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("联系人管理")
@Controller
@RequestMapping("/api/linkman")
public class LinkmanController {

    @Autowired
    private CstLinkmanServiceImpl cstLinkmanService;

    //查询客户联系人
    @RequestMapping("findCstLinkmanList")
    public String findCstLinkmanList(String lkmCustNo,String custName, HttpServletRequest request) throws UnsupportedEncodingException {
        QueryWrapper<CstLinkman> wrapper = new QueryWrapper<>();
        wrapper.eq("lkm_cust_no",lkmCustNo);
        List<CstLinkman> linkmens = cstLinkmanService.list(wrapper);
        request.setAttribute("linkmens",linkmens);
        request.setAttribute("lkmCustNo",lkmCustNo);
        request.setAttribute("custName",custName);
        return "linkman/list";
    }

    //根据编号查询联系人
    @RequestMapping("getCstLinkman")
    public String getCstLinkman(Long lkmId,String custName,HttpServletRequest request) throws UnsupportedEncodingException {
        CstLinkman cstLinkman = cstLinkmanService.getById(lkmId);
        request.setAttribute("cstLinkman",cstLinkman);
        request.setAttribute("custName",custName);
        return "linkman/edit";
    }

    //编辑客户联系人
    @RequestMapping("editCstLinkman")
    public String editCstLinkman(CstLinkman cstLinkman) throws UnsupportedEncodingException {
        boolean flag = cstLinkmanService.updateById(cstLinkman);
        if(flag){                                                        //对参数进行编码，防止乱码
            return "redirect:/api/linkman/findCstLinkmanList?custName="+ URLEncoder.encode(cstLinkman.getLkmCustName(),"UTF-8") + "&lkmCustNo="+cstLinkman.getLkmCustNo();
        }
        return "redirect:/api/linkman/getCstLinkman?lkmId="+cstLinkman.getLkmId();
    }

    //删除客户联系人
    @RequestMapping("delCstLinkman/{lkmId}")
    @ResponseBody
    public Map delCstLinkman(@PathVariable Integer lkmId){
        Map map = new HashMap();
        boolean flag = cstLinkmanService.removeById(lkmId);
        map.put("result",flag);
        return map;
    }

    //新增跳转
    @RequestMapping("addCstLinkman")
    public String addCstLinkman(String lkmCustNo,String custName,HttpServletRequest request){
        request.setAttribute("lkmCustNo",lkmCustNo);
        request.setAttribute("custName",custName);
        return "linkman/add";
    }

    //新建客户联系人
    @RequestMapping("saveCstLinkman")
    public String saveCstLinkman(CstLinkman cstLinkman) throws UnsupportedEncodingException {
        boolean save = cstLinkmanService.save(cstLinkman);
        if(save){
            return "redirect:/api/linkman/findCstLinkmanList?custName="+ URLEncoder.encode(cstLinkman.getLkmCustName(),"UTF-8") + "&lkmCustNo="+cstLinkman.getLkmCustNo();
        }
        return "redirect:/api/linkman/addCstLinkman";
    }
}
